<?php

/**
 * 后台 Tag模块 Controller
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007@gmail.com $  <zwttmac@qq.com>
 * $Id: tags.php 73 2011-11-22 12:50:33Z zwt007@gmail.com $
 * http://www.t-mac.org；
 */
class tagsAction extends Action
{

    private $tmp_model;
    private $archives_model;

    /**
     * _init 方法 在执行任何Action前执行
     */
    public function _init()
    {
        $this->assign('action', $_GET['TMAC_ACTION']);
        $this->tmp_model = Tmac::model('tags');

        $check_model = $this->M('check');
        $check_model->checkLogin();
        $check_model->CheckPurview('tb_admin,tb_editor');
    }

    /**
     * 资讯类别管理 首页
     */
    public function index()
    {
        $search_keyword = $this->H($this->getParam('search_keyword'));
        $do = $this->H($this->getParam('do'));

        //TODO  取出所有资讯
        $rs = $this->tmp_model->getTagsList($search_keyword);

        //取友情操作类型radiobutton数组
        $Tags_do_ary = Tmac::config('article.do');
        $Tags_do_ary_option = Utility::Option($Tags_do_ary, $do);

        $this->assign('search_keyword', $search_keyword);
        $this->assign('tags_do_ary_option', $Tags_do_ary_option);
        $this->assign($rs);        
        $this->V('tags');
    }

    /**
     * 新增/修改文章页面
     */
    public function add()
    {
        $tag_id = intval($this->getParam('tagid'));

        if ($tag_id > 0) {
            $editinfo = $this->tmp_model->getTagsInfo($tag_id);
        } else {
            $editinfo = array(
                'tag_id' => '',
                'tag_name' => ''
            );
        }


        $this->assign('editinfo', $editinfo);
        $this->V('tags');
    }

    /**
     * 新增/修改栏目页面　保存　
     */
    public function save()
    {
        if (empty($_POST) || count($_POST) < 1) {
            $this->redirect('don\'t be evil');
            exit;
        }

        /* 初始化变量 */
        $tagid = intval($this->getParam('tag_id'));
        $tag_name = $this->H($this->getParam('tag_name'));


        //判断是否为空
        if (empty($tag_name)) {
            $this->redirect('Tags标题不能为空');
            exit;
        }

        $info = array(
            'tag_name' => $tag_name
        );

        $tag_page = HttpResponse::getCookie('tags_page');
        $type_url = PHP_SELF . '?m=tags&page=' . $tag_page;
        if ($tagid > 0) {
            $checkTags = $this->tmp_model->checkTags($tagid, $tag_name);
            if (!$checkTags) {
                $this->redirect('此Tags已经存在啦!');
                exit;
            }
            //update save Tags
            $where = 'tag_id = ' . $tagid . '';
            $rs = $this->tmp_model->addTags($info, 'UPDATE', $where);
            $rs = $this->tmp_model->addTagsInfo($info, 'UPDATE', $where);
            if ($rs) {
                $this->redirect('修改Tags成功', $type_url);
            } else {
                $this->redirect('修改Tags失败');
            }
        } else {
            //insert save Tags_class
            $rs = $this->tmp_model->addTags($info);
            if ($rs) {
                $this->redirect('新增Tags成功', $type_url);
            } else {
                $this->redirect('新增Tags失败');
            }
        }
    }

    /**
     * 批量操作
     */
    public function tags_do()
    {
        $do = $this->getParam('do');
        $id_a = $_POST['id_a'];

        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } else {
            $this->redirect('请选择要操作的...');
            exit;
        }

        if ($do == 'del') {
            $rs = $this->tmp_model->delTags($id);
            $rs = $this->tmp_model->delTagsInfo($id);
            // TODO DEL该分类下的所有资讯
            if ($rs) {
                $this->redirect('删除Tags成功');
            } else {
                $this->redirect('删除Tags失败，请重试！');
            }
        }
    }

    /**
     * del
     * @param int $class_id
     */
    public function del()
    {
        $aid = (int) $_GET['tagid'];
        $id_a = $this->getParam('id_a');
        if (is_array($id_a)) {
            $id = implode(',', $id_a);
        } elseif (!empty($aid)) {
            $id = $aid;
        } else {
            $this->redirect('请选择要删除的...');
            exit;
        }
        $rs = $this->tmp_model->delTags($id);
        $rs = $this->tmp_model->delTagsInfo($id);
        // TODO DEL该分类下的所有资讯
        if ($rs) {
            $this->redirect('删除Tags成功');
        } else {
            $this->redirect('删除Tags失败，请重试！');
        }
    }

    Public function ajaxGetTags()
    {
        $rs = $this->tmp_model->getTagsArray();
        $array = json_encode($rs);
        echo $array;
    }

    public function ajaxSaveTags()
    {
        $tag_name_string = $this->H($this->getParam('tag_name'));
        $tag_name = explode(',', $tag_name_string);
        $tag_array = array();
        if (is_array($tag_name)) {
            foreach ($tag_name AS $k => $v) {
                //判断是否存在
                $rs = $this->tmp_model->getTagsInfoByName($v);
                if (!$rs) {
                    $info = array(
                        'tag_name' => $v
                    );
                    $tag_id = $this->tmp_model->addTagsReturn($info);
                    if ($tag_id) {
                        $tag_array[$tag_id] = $v;
                    }
                } else {
                    $tag_array[$rs['tag_id']] = $rs['tag_name'];
                }
            }
        }

        $tag_return_string = json_encode($tag_array);
        echo $tag_return_string;
    }

}